package com.axend.aerosense.user.utils;

import android.os.CountDownTimer;
import android.widget.Button;

import com.axend.aerosense.user.R;


/**
 * 获取验证码按钮倒计时
 * @author ChenHongWei
 * @date :2020/12/24 17:54
 */

public class UserBtnTimeCountDown extends CountDownTimer {

    private Button button;

    /**
     *
     * @param button  按钮
     * @param second  秒
     */
    public UserBtnTimeCountDown(Button button, int second) {
        //获取验证码按钮一般为60秒倒计时
        super(1000 * second, 1000);
        this.button = button;
    }

    //开始倒计时
    public void btnStart(){
        start();
        button.setEnabled(false);
    }

    //停止计时
    public void btnStop(){
        cancel();
        button.setEnabled(true);
    }

    @Override
    public void onTick(long millisUntilFinished) {
        if (button != null){
            button.setText(millisUntilFinished / 1000 + "");
        }
    }

    @Override
    public void onFinish() {
        if (button != null){
            button.setEnabled(true);
            button.setText(R.string.user_get_verification_code);
        }
    }
}
